package cn.example.demo.modules.house.service.impl;

import cn.example.demo.modules.house.dto.ScenerySpotDto;
import cn.example.demo.modules.house.entity.ScenerySpot;
import cn.example.demo.modules.house.mapper.ScenerySpotMapper;
import cn.example.demo.modules.house.service.IScenerySpotService;
import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.context.LoginUserApi;
import cn.example.demo.common.tools.QueryServiceUtils;
import cn.example.demo.common.tools.obj.reflect.EntityUtils;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 描述：【旅游景点】接口实现类
 */

@Service
public class ScenerySpotServiceImpl extends ServiceImpl<ScenerySpotMapper, ScenerySpot> implements IScenerySpotService {
    @Autowired
    private LoginUserApi loginUserApi;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult insertScenerySpot(ScenerySpotDto scenerySpotDto) {
        ScenerySpot entity = EntityUtils.entityConvert(scenerySpotDto, new ScenerySpot(), false);

        if (this.save(entity)) {
            return ServiceResult.isSuccess(StrUtil.format("【旅游景点，ID:{}】创建成功!", entity.getId()), entity);
        }

        return ServiceResult.isInternalError("【旅游景点】创建失败，未知错误！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult updateScenerySpot(ScenerySpotDto scenerySpotDto) {
        ScenerySpot entity = this.getById(scenerySpotDto.getId());
        if (entity == null) {
            return ServiceResult.isNotFound(StrUtil.format("【旅游景点】信息更新失败：记录不存在！ID:{}", scenerySpotDto.getId()));
        }

        entity = EntityUtils.entityConvert(scenerySpotDto, entity, true);
        if (this.updateById(entity)) {
            return ServiceResult.isSuccess(StrUtil.format("【旅游景点，ID:{}】信息更新成功!", entity.getId()), null);
        }

        return ServiceResult.isNotModified("未知错误，【旅游景点】信息修改失败！");
    }

    @Override
    public PageBean queryScenerySpot(ScenerySpotDto scenerySpotDto) {
        Page pageResult = PageHelper.startPage(scenerySpotDto.getPageNum(), scenerySpotDto.getPageSize())
                .doSelectPage(() -> this.list(getQueryWrapper(scenerySpotDto)));

        return QueryServiceUtils.encapsulatePageBean(new PageBean<>(), pageResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult deleteScenerySpot(ScenerySpotDto scenerySpotDto) {
        int rows = this.baseMapper.deleteBatchIds(scenerySpotDto.getIds());
        if (rows > 0) {
            return ServiceResult.isSuccess(StrUtil.format("操作成功，已批量删除【旅游景点】共 {} 条记录！", rows), rows);
        }

        return ServiceResult.isNotModified("删除【旅游景点】失败，请稍后重试！");
    }

    @Override
    public ScenerySpot findScenerySpotById(String id) {
        return this.getById(id);
    }

    @Override
    public List<Map<String, Object>> queryScenerySpotAsDict(ScenerySpotDto scenerySpotDto) {
        return this.baseMapper.selectAsDictList(getQueryWrapper(scenerySpotDto));
    }

    /**
     * 查询条件
     *
     * @param scenerySpotDto
     * @return
     */

    protected LambdaQueryWrapper getQueryWrapper(ScenerySpotDto scenerySpotDto) {
        LambdaQueryWrapper<ScenerySpot> wrapper = Wrappers.lambdaQuery();

        wrapper
                .eq(StrUtil.isNotBlank(scenerySpotDto.getId()), ScenerySpot::getId, scenerySpotDto.getId())

                .like(StrUtil.isNotBlank(scenerySpotDto.getName()), ScenerySpot::getName, scenerySpotDto.getName())

                .like(StrUtil.isNotBlank(scenerySpotDto.getSceneryType()), ScenerySpot::getSceneryType, scenerySpotDto.getSceneryType())

                .like(StrUtil.isNotBlank(scenerySpotDto.getSceneryImageUrl()), ScenerySpot::getSceneryImageUrl, scenerySpotDto.getSceneryImageUrl())

                .like(StrUtil.isNotBlank(scenerySpotDto.getAnnounceTitle()), ScenerySpot::getAnnounceTitle, scenerySpotDto.getAnnounceTitle())

                .eq(scenerySpotDto.getDetailArticle() != null, ScenerySpot::getDetailArticle, scenerySpotDto.getDetailArticle())

                .eq(scenerySpotDto.getSceneryStatus() != null, ScenerySpot::getSceneryStatus, scenerySpotDto.getSceneryStatus())
        ;

        return wrapper;
    }
}
